<script type="text/javascript">
console.log($("#form_op").form().length);
if($("#form_op").form().length>1){
	console.log("帮助easyui删除隐藏的element");
	$("#op_popwin").remove();
}
</script>
<table id="user_pages" url="api/user/page_data" iconCls="icon-man"
	pageSize="20" rownumbers="true" pagination="true" singleSelect="true"
	toolbar="#op_tb">
	<thead>
		<tr>
			<th field="code" width="100">用户代码</th>
			<th field="name" width="100">用户名</th>
			<th field="jgId" width="150" align="right">单位代码</th>
			<th field="jgName" width="240" align="right">单位名称</th>
			<th field="deptName" width="150" align="right">所在部门</th>
			<th field="roleName" width="120" align="right">用户角色</th>
		</tr>
	</thead>
</table>
<div id="op_tb">
    <a href="#" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="javascript:myapp.pageid_opmanage.add();">新增</a>    
    <a href="#" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="javascript:myapp.pageid_opmanage.edit();">编辑</a>   
    <a href="#" class="easyui-linkbutton" iconCls="icon-cut" plain="true" onclick="javascript:myapp.pageid_opmanage.del();">删除</a>
    <input id="filter" class="easyui-textbox" data-options="prompt:'用户代码、用户名；单位代码、名称；模糊查询'" style="width:360px">
     <a href="#" class="easyui-linkbutton" iconCls="icon-search" plain="true" onclick="javascript:myapp.pageid_opmanage.search();">查找</a>
</div>
<div id="op_popwin" class="easyui-dialog" style="width:550px;height:370px;padding:10px 30px;"
        title="操作员信息管理" buttons="#dlg-buttons" data-options="modal:true,closed:true" iconCls="icon-man">
    <h2>操作员信息</h2>
    <form id="form_op" method="post">
    	<input type="text" name="id" style="width:30px;display:none"/>
        <table>
        	<tr>
                <td>用户代码：</td>
                <td><input class="easyui-textbox" name="code"  data-options="required:true" style="width:360px;"/></td>
            </tr>
            <tr>
                <td>  用户名：</td>
                <td><input   class="easyui-textbox" name="name"  data-options="required:true" style="width:360px;"/></td>
            </tr>
            <tr>
            	<td>用户角色：</td>
            	<td><input id="role" name="roleId" class="easyui-tagbox"   data-options="
						valueField: 'roleId',
						textField: 'roleName',
						editable:false,
						multiple:true,
						limitToList:true,
		    			hasDownArrow:true,
						data: [{
							roleId: '001',
							roleName: '管理员'
						},
						{
							roleId: '002',
							roleName: '操作员'
						}]" style="width:360px;"/>
				</td>
            </tr>
            
             <tr>
                <td>所在单位：</td>
                <td><select id="pbc"  class="easyui-combotree" url="api/user/getmysonpbc" data-options="required:true" name="jgId" style="width:360px;"/></td>
            </tr>
            <tr>
                <td>所在部门：</td>
                <td><select id="userDept" class="easyui-combotree" name="deptId"  data-options="required:true" style="width:360px;"/></td>
            </tr>
             <tr>
                <td>邮箱名称：</td>
                <td><select id="emailAddress" class="easyui-textbox"   name="emailAddress" style="width:360px;"/></td>
            </tr>
               <input name="emailPassword"  type="text" style="width:30px;display:none"/>  
            <tr id="setpass"><td></td>
            <td align="right"><input type="checkbox" name="setpass" value="true" />重置口令</td>
            </tr>
            <tr id="setpass">
            <td align="right"  colspan="2"></td>
            </tr>              
        </table>
    </form>    
   
</div>
 <div id="dlg-buttons">
	<span id="prompt_opmanage" style="color: red; display: none">提示信息</span>
    <a href="#" class="easyui-linkbutton" iconCls="icon-ok" onclick="myapp.pageid_opmanage.saveOP()">保存</a>
    <a href="#" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#op_popwin').dialog('close')">取消</a>
	</div>
<script type="text/javascript">
$(document).ready(function (){
     
	/* if($.op.roleId.indexOf('001')>-1)
		{
			$("#role").tagbox({data:[
			{
				roleId: '001',
				roleName: '管理员'
			},
			{
				roleId: '002',
				roleName: '操作员'
			} ]
			});
		}
	else
		{
			$("#role").tagbox({data:[
				{
					roleId: '002',
					roleName: '部门管理员'
				},
				{
					roleId: '003',
					roleName: '操作员'
				}]
		});
	  } */
	$("#user_pages").datagrid({
		onDblClickRow:function(index,row){
			$("#setpass").show();
			$("[name='setpass']").removeAttr("checked");
			$("#role").combobox('setValues',[]);
		    $("#form_op").form("load",row);
		    var url='api/user/getJgDeptList?jgId='+row.jgId;
		    $.get(url, function(result){
		    	 $('#userDept').combotree({data:result.data});
		    	 $('#userDept').combotree('setValue',row.deptId);
		      });
			$("#op_popwin").dialog("open");
		}
	});
	$('#pbc').combotree({
	    onChange: function(rec){
	            var url = 'api/user/getJgDeptList?jgId='+rec;
	            $('#userDept').combotree('clear');
	            $('#userDept').combotree('reload', url);
	       }
	});
	/* $('#userDept').combotree({
	     url :'api/user/getJgDeptList?jgId='+$('#pbc').combotree('getValue')
	}); */
	
});
	var myapp=myapp||{};
	$('#form_op').form({
		url : 'api/user/save',
		onSubmit : function(param) {
			var row = $('#user_pages').datagrid('getSelected');
			if (row) {
				param.password = row.password;
			}
			var codes = $("#role").combobox("getValues");
			var nameStr = "";
			if (codes.length > 0){
				if (codes.length == 1){
					nameStr = getNameByCode(codes[0]);
				}else{
					for (var i = 0; i < codes.length - 1; i ++){
						nameStr += getNameByCode(codes[i]) + ",";
					}
					nameStr += getNameByCode(codes[codes.length - 1]);
				}
			}
			param.roleName = nameStr;
			param.jgName = $("#pbc").combotree("getText");
			param.deptName = $("#userDept").combotree("getText");
			console.log(param);
		},
		success : function(data) {
			var data = JSON.parse(data);
			//console.log(data.data);
			if (data.code == 1) {
				//console.log(data.msg);
				$.prompt(data.msg, 'prompt_opmanage');
			} else {
				$.messager.alert("提示",data.msg);
				$('#op_popwin').dialog('close');
				$("#user_pages").datagrid("reload");
			}
		}
	});
	myapp.pageid_opmanage = {
		saveOP : function() {
			 var isValid =$("#form_op").form('validate');
		      if(!isValid){
		          return false;
		      }
			$('#form_op').submit();
		},
		search : function() {
			var filter = $("#filter").textbox("getText");
			var wherestr = "";
			if (filter != "") {
				wherestr = "and (code like '%" + filter
						+ "%' or name like '%" + filter
						+ "%' or jgId like '%" + filter
						+ "%' or jgName like '%" + filter + "%')";
			}
			$('#user_pages').datagrid("load", {
				filter : wherestr
			});
		},
		add : function() {
			$("#setpass").hide();
			$("[name='setpass']").attr("checked", 'true');
			$('#form_op').form('reset');
			$("#role").combobox('setValues',[]);
			$("#op_popwin").dialog("open");
		},
		edit : function() {
			var row = $('#user_pages').datagrid('getSelected');
			if (row) {
				$("#setpass").show();
				$("[name='setpass']").removeAttr("checked");
				$("#role").combobox('setValues',[]);
				$("#form_op").form("load", row);
				$("#op_popwin").dialog("open");
			} else {
				$.messager.tips("请鼠标点击要编辑的行。");
			}
		},
		del : function() {
			var row = $('#user_pages').datagrid('getSelected');
			if (row) {
				$.messager.confirm('系统提示', ' 你确认要删除 ' + row.name + '的数据么?',
						function(r) {
							if (r) {
								$.get('api/user/delete', {
									id : row.id
								}, function(data) {
									$("#user_pages").datagrid("reload");
								});
							}
						});
			} else {
				$.messager.tips("请鼠标点击选择要删除的行！");
			}
		}
	}
	function getNameByCode(code){
		var name="";
		switch (code) {
		case "001":
			name="管理员";
			break;
		case "002":
			name="操作员";
			break;
		default:
			break;
		}
		return name;
	}
	//函数名不能用delete，否则ie8出错，因此改名为 del
</script>
